//
//  SWTabBarController.swift
//  SinaWeibo
//
//  Created by wszyxc on 16/7/17.
//  Copyright © 2016年 wszyxc. All rights reserved.
//

import UIKit

class SWTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //添加所有的childVC
        addAllChildVC()
    
        //设置tabBar为自定义的tabBar
        setupTabBar()
        
    }
    
    //设置tabbar
    func setupTabBar(){
        
        let tabBar = SWTabBar()
        
        setValue(tabBar, forKey: "tabBar")
        
        //在这里给tabBar里面的闭包赋值,因为只有在这里才能拿到tabbar里面的闭包
        
//        tabBar.closure = {
//            
//            print("加号被点击")
//        }
        
        //这里设置tabBar的代理
        
        tabBar.swDelegate = self
        
    }
    
    /**
     添加所有的childVC
     */
    func addAllChildVC(){
        
        //添加首页控制器
        let homeVC = SWHomeController()
        
        addChildViewController(homeVC, imageName: "tabbar_home", selectedImageName: "tabbar_home_selected", title:"首页")
        
        //添加消息控制器
        let messageVC = SWMessageController()
        
        addChildViewController(messageVC, imageName: "tabbar_message_center", selectedImageName: "tabbar_message_center_selected",title: "消息")
        
        //添加发现
        
        let discoverVC = SWDicoverController()
        
        addChildViewController(discoverVC, imageName: "tabbar_discover", selectedImageName: "tabbar_discover_selected", title: "发现")
        
        //添加我
        let profileVC = SWProfileController()
        
        addChildViewController(profileVC, imageName: "tabbar_profile", selectedImageName: "tabbar_profile_selected", title: "我")
        
    }
    
    /**
     添加一个childVC
    */
    func addChildViewController(childController: UIViewController, imageName:String, selectedImageName:String, title:String) {
        
        childController.title = title
        
        childController.tabBarItem.image = UIImage(named: imageName)?.imageWithRenderingMode(.AlwaysOriginal)
        
        childController.tabBarItem.selectedImage = UIImage(named: selectedImageName)?.imageWithRenderingMode(.AlwaysOriginal)
        
        var mdict:[String:AnyObject] = [String:AnyObject]()
        
        mdict[NSForegroundColorAttributeName] = UIColor.orangeColor()
    
        childController.tabBarItem.setTitleTextAttributes(mdict, forState: .Selected)
        
        addChildViewController(UINavigationController(rootViewController: childController))
        
        
        
        
    }


}

//定义一个分类 用来处理代理的事宜
extension SWTabBarController:SWTabBarDelegate{
    
    func composeBtnClick() {
        print("加号按钮被点击 代理监听到")
        
        let nav = UINavigationController(rootViewController: SWCenterController())
        
        //modal到 centerVC
        presentViewController(nav, animated: true) { () -> Void in
            
        }
        
               
    }
    
    
}








